home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11376 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: ix.netcom.com!netnews
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: RTTI and MSVC 4.0 examples sought
  5. Date: 14 Mar 1996 05:03:50 GMT
  6. Organization: Netcom
  7. Message-ID: <4i89bm$57m@ixnews3.ix.netcom.com>
  8. References: <313F912F.39D2@vream.com> <4hpv0r$7sv@druid.borland.com>
  9. NNTP-Posting-Host: den-co20-12.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Wed Mar 13  9:03:50 PM PST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <4hpv0r$7sv@druid.borland.com>, pete@borland.com says...
  16. >
  17. >In article <313F912F.39D2@vream.com>, tnewark@vream.com says...
  18. >>
  19. >>I'm looking for some examples of using RTTI with MSVC 4.0.
  20. >>
  21. >>The problem I'm specifically trying to solve is that I want to implement
  22. >>IsKindOf(Ptr, class) where I return true if a ptr is type class or a 
  23. >>subclass of class, and false otherwise.
  24. >>
  25. >>MicroSoft's documentation is very weak on the RTTI subject and type_info 
  26. >>class, especially about such things as collating order (what exactly is 
  27. >>it?)
  28. >
  29. >#define IsKindOf(Ptr,class) dynamic_cast<class *>(Ptr)
  30. >}
  31. >
  32. >I haven't tried this with MSVC 4.0. However, it should work on any compiler 
  33. >that implements RTTI correctly.
  34. >
  35.  
  36. Be careful here...  the VC++ 4.0 docs state the the run-time type info
  37. used for MFC is NOT C++ RTTI, but some other mechanism.  IsKindOf in
  38. MFC uses the home-grown RTTI, not C++ RTTI.  It takes a pointer to
  39. CRunTimeClass, which can be obtained with the RUNTIME_CLASS macro,
  40. and I quoth from the doc...
  41.  
  42. RUNTIME_CLASS( class_name )
  43. Parameters
  44. class_name   The actual name of the class (not enclosed in quotation marks).
  45. Remarks
  46. Use this macro to get the run-time class structure from the name of a C++ 
  47. class.
  48. RUNTIME_CLASS returns a pointer to a CRuntimeClass structure for the class 
  49. specified by class_name. Only CObject-derived classes declared with 
  50. DECLARE_DYNAMIC, DECLARE_DYNCREATE, or DECLARE_SERIAL will return pointers to 
  51. a CRuntimeClass structure.
  52. For more information, see the article CObject Class in Programming with MFC.
  53. Example
  54. // example for RUNTIME_CLASS
  55. CRuntimeClass* prt = RUNTIME_CLASS( CAge );
  56. ASSERT( lstrcmp( prt->m_lpszClassName, "CAge" )  == 0 );
  57.   
  58.  
  59. So to integrate with MFC's runtime-type-info, derive from CObject and use
  60. one of the DECLARE_* magic macros.
  61.  
  62. john lilley
  63.  
  64.